/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jesstalk.chatsystem;

import jesstalk.chatsystem.chatrequest.ChatRequest;
import com.google.gson.Gson;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jesstalk.Db;

/**
 *
 * @author Ecky
 */
public class ChatServlet extends HttpServlet {

    private ChatController _chatController;

    public ChatServlet() {
        try {
            _chatController = new ChatController(new Db());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ChatServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ChatServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("info") != null) {
            response.getWriter().write(new Gson().toJson(_chatController.processInfo(request.getParameter("info"))));
        }

        if(request.getParameter("deluserfromsystem") != null) {
            _chatController.delUserFromSystem(request.getParameter("deluserfromsystem"));
        }

        if(request.getParameter("setuserasmod") != null) {
            _chatController.setUserAsMod(request.getParameter("username"), Integer.parseInt(request.getParameter("categId")));
        }

        if(request.getParameter("banuserfromsystem") != null) {
            _chatController.banUserFromSystem(request.getParameter("banuserfromsystem"));
        }

        if(request.getParameter("delcategoryfromsystem") != null) {
            _chatController.delCategoryFromSystem();
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if(request.getParameter("data") != null) {
            try {
                ChatRequest req = new Gson().fromJson(request.getParameter("data"), ChatRequest.class);
                response.getWriter().write(new Gson().toJson(_chatController.processChatRequest(req)));
            } catch (SQLException ex) {
                response.getWriter().write(new Gson().toJson(ex));
            }
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Chatting server";
    }// </editor-fold>

}
